package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("menu")
public class MenuController {
    @Reference//用这个注解来远程调用service而不是Autowired
    private MenuService menuService;

    @RequestMapping("toShow")
    public String toShow(){
        return "index";
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }
    @RequestMapping("toztr")
    public String toGetZtreev(){
        return "ztreev";
    }
    @RequestMapping("getZv")
    @ResponseBody
    public List<Map<String,Object>> getZv(){
        List<Map<String,Object>> list = menuService.getZv();
        return list;
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public List<Map<String,Object>> getInfo(HttpServletRequest request){
        TPerson user = (TPerson)SecurityUtils.getSubject().getPrincipal();
        List<Map<String,Object>> list = menuService.getInfo(user.getPid());
        return list;
    }
    @RequestMapping("addInfo")
    @ResponseBody
    public boolean addInfo (Menu menu){
        boolean b = menuService.addInfo(menu);
        return b;
    }
    @RequestMapping("toupdate/{id}")
    public String toupdate(@PathVariable("id")Integer id, Model model){
       Menu m =  menuService.toUpdate(id);
       model.addAttribute("m",m);
        return "update";
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public Boolean updateInfo(Menu menu){
        boolean b = menuService.updateInfo(menu);
        return b;
    }
    @RequestMapping("deleteInfo/{id}")
    public int deleteInfo(@PathVariable("id")Integer id){
        int b = menuService.deleteInfo(id);
        return b;
    }
    @RequestMapping("getZvChecked/{roleId}")
    @ResponseBody
    public List<Map<String,Object>> getZvChecked(@PathVariable("roleId")Integer roleId){
        List<Map<String,Object>> list = menuService.getZvChecked(roleId);
        return list;
    }
}
